package com.songyl.service.impl;

import com.songyl.entity.Book;
import com.songyl.mapper.BookMapper;
import com.songyl.service.IServiceInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SYL
 * @since 2022-03-20
 */
@Service
public class BookServiceImpl implements IServiceInterface<Book> {

    @Autowired
    BookMapper bookMapper;

    @Override
    public int save (Book book) {
        return bookMapper.save(book)
                .getId().equals(book.getId())?1:0;
    }

    @Override
    public int delete (Integer id) {
        bookMapper.findById(id).ifPresent(
                value -> {
                    bookMapper.deleteById(id);
                }
        );
        return (!bookMapper.existsById(id))?1:0;
    }

    @Override
    public List<Book> findAll () {
        return bookMapper.findAll();
    }

    @Override
    public Book findById (Integer id) {
        return bookMapper.findById(id).orElse(null);
    }

    @Override
    public long count () {
        //jpa  自带的count方法 select count(*)
        return bookMapper.count();
    }

    /**
     * @param ISBN
     * @return
     */
    public Book findBookByISBN (String ISBN) {

        return bookMapper.findBookByISBN(ISBN);
    }
}
